using System;
using ValidationFramework.Configuration.Reflection;

namespace ValidationFramework.Specifications
{
	/// <summary>
	/// Specifies that a <see cref="ValidatableRule"/> should be applied to the program element.
	/// </summary>
	/// <seealso cref="ValidatableRule"/>
	/// <example>
	/// <code source="Examples\ExampleLibraryCSharp\Specifications\ValidatableRuleAttributeExample.cs" lang="cs"/>
	/// <code source="Examples\ExampleLibraryVB\Specifications\ValidatableRuleAttributeExample.vb" lang="vbnet"/>
	/// </example>
#if (!SILVERLIGHT)
	[Serializable]
#endif
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
	public sealed class IsValidAttribute : RuleAttribute
	{
		// --- Constructor 
		public IsValidAttribute()
		{
			UseMemberErrorMessages = false;
			MemberRuleSet = string.Empty;
		}
		// --- Properties

		/// <summary>
		/// Gets or sets a value indicating if <see cref="IValidatable.ErrorMessages"/> should be use as the <see cref="Rule.ErrorMessage"/>.  
		/// </summary>
		public bool UseMemberErrorMessages
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the ruleset to check this member against
		/// </summary>
		public string MemberRuleSet { get; set; }


		// --- Methods

		public override ISpecification CreateSpecification(ICachedReflectionInfo descriptor)
		{
			var isValidadSpecification = new IsValidSpec();
			isValidadSpecification.TargetRuleSet = MemberRuleSet;
			isValidadSpecification.UseTargetErrorMessages = UseMemberErrorMessages;

			return isValidadSpecification;
		}
	}
}